home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Network / Hosts.pm next >
Encoding:
Perl POD Document  |  2009-04-09  |  12.4 KB  |  507 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # Hosts Configuration handling
  4. #
  5. # Copyright (C) 2000-2001 Ximian, Inc.
  6. #
  7. # Authors: Hans Petter Jansson <hpj@ximian.com>
  8. #          Carlos Garnacho     <carlosg@gnome.org>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Library General Public License as published
  12. # by the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. # GNU Library General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Library General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  23.  
  24. package Network::Hosts;
  25.  
  26. sub run_hostname
  27. {
  28.   my ($hostname) = @_;
  29.  
  30.   &Utils::Report::enter ();
  31.   &Utils::Report::do_report ("network_hostname_set", $hostname);
  32.   &Utils::File::run ("hostname $hostname");
  33.   &Utils::Report::leave ();
  34. }
  35.  
  36. sub get_fqdn_dist
  37. {
  38.   my %dist_map =
  39.      (
  40.     "redhat-6.2"      => "redhat-6.2",
  41.     "redhat-7.0"      => "redhat-6.2",
  42.     "redhat-7.1"      => "redhat-6.2",
  43.     "redhat-7.2"      => "redhat-7.2",
  44.     "redhat-8.0"      => "redhat-7.2",
  45.     "mandrake-9.0"    => "redhat-6.2",
  46.     "yoper-2.2"       => "redhat-6.2",
  47.     "conectiva-9"     => "redhat-6.2", 
  48.     "debian-3.0"      => "debian-2.2",
  49.     "debian-3.1"      => "debian-2.2",
  50.     "debian-4.0"      => "debian-2.2",
  51.     "debian-5.0"      => "debian-2.2",
  52.     "debian-testing"  => "debian-2.2",
  53.     "ubuntu-7.04"     => "debian-2.2",
  54.     "suse-9.0"        => "suse-9.0",
  55.     "pld-1.0"         => "redhat-6.2",
  56.     "vine-3.0"        => "redhat-6.2",
  57.     "ark"             => "redhat-6.2",
  58.     "slackware-9.1.0" => "suse-9.0",
  59.     "gentoo"          => "gentoo",
  60.     "freebsd-5"       => "freebsd-5",
  61.     "solaris-2.11"    => "solaris-2.11",
  62.       );
  63.  
  64.   return $dist_map{$Utils::Backend::tool{"platform"}};
  65. }
  66.  
  67. sub get_fqdn_parse_table
  68. {
  69.   my %dist_tables =
  70.     (
  71.      "redhat-6.2" =>
  72.      {
  73.        fn =>
  74.        {
  75.          SYSCONFIG_NW => "/etc/sysconfig/network",
  76.          RESOLV_CONF  => "/etc/resolv.conf"
  77.        },
  78.        table =>
  79.        [
  80.         [ "hostname", \&Utils::Parse::get_sh, SYSCONFIG_NW, HOSTNAME ],
  81.         [ "domain",   \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  82.        ]
  83.      },
  84.  
  85.      "redhat-7.2" =>
  86.      {
  87.        fn =>
  88.        {
  89.          SYSCONFIG_NW => ["/etc/sysconfig/networking/profiles/default/network",
  90.                           "/etc/sysconfig/networking/network",
  91.                           "/etc/sysconfig/network"],
  92.          RESOLV_CONF  => ["/etc/sysconfig/networking/profiles/default/resolv.conf",
  93.                           "/etc/resolv.conf"],
  94.        },
  95.        table =>
  96.        [
  97.             [ "hostname", \&Utils::Parse::get_sh, SYSCONFIG_NW, HOSTNAME ],
  98.             [ "domain",   \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  99.            ]
  100.      },
  101.  
  102.      "debian-2.2" =>
  103.      {
  104.        fn =>
  105.        {
  106.          RESOLV_CONF => "/etc/resolv.conf",
  107.          HOSTNAME    => "/etc/hostname",
  108.        },
  109.        table =>
  110.        [
  111.         [ "hostname", \&Utils::Parse::get_first_line, HOSTNAME ],
  112.         [ "domain",    \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  113.        ]
  114.      },
  115.  
  116.      "suse-9.0" =>
  117.      {
  118.        fn =>
  119.        {
  120.          RESOLV_CONF  => "/etc/resolv.conf",
  121.          HOSTNAME     => "/etc/HOSTNAME",
  122.        },
  123.        table =>
  124.        [
  125.         [ "hostname", \&Utils::Parse::get_fq_hostname, HOSTNAME ],
  126.         [ "domain", \&Utils::Parse::get_fq_domain, HOSTNAME ],
  127.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  128.        ]
  129.      },
  130.     
  131.      "gentoo" =>
  132.      {
  133.        fn =>
  134.        {
  135.          HOSTNAME    => "/etc/conf.d/hostname",
  136.          DOMAINNAME  => "/etc/conf.d/domainname",
  137.          RESOLV_CONF => "/etc/resolv.conf",
  138.        },
  139.        table =>
  140.        [
  141.         [ "hostname", \&Utils::Parse::get_sh, HOSTNAME, HOSTNAME ],
  142.         [ "domain", \&Utils::Parse::get_sh, DOMAINNAME, DNSDOMAIN ],
  143.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  144.        ]
  145.      },
  146.  
  147.      "freebsd-5" =>
  148.      {
  149.        fn =>
  150.        {
  151.          RC_CONF     => "/etc/rc.conf",
  152.          RESOLV_CONF => "/etc/resolv.conf",
  153.        },
  154.        table =>
  155.        [
  156.         [ "hostname", \&Utils::Parse::get_sh_re, RC_CONF, hostname, "^([^\.]*)\." ],
  157.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  158.        ]
  159.      },
  160.  
  161.      "solaris-2.11" =>
  162.      {
  163.        fn =>
  164.        {
  165.          NODENAME    => "/etc/nodename",
  166.          RESOLV_CONF => "/etc/resolv.conf",
  167.        },
  168.        table =>
  169.        [
  170.         [ "hostname", \&Utils::Parse::get_first_line, NODENAME ],
  171.         [ "domain", \&Utils::Parse::split_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  172.        ]
  173.      },
  174.    );
  175.  
  176.   my $dist = &get_fqdn_dist ();
  177.   return %{$dist_tables{$dist}} if $dist;
  178.  
  179.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  180.   return undef;
  181. }
  182.  
  183. sub get_fqdn_replace_table
  184. {
  185.   my %dist_tables =
  186.     (
  187.      "redhat-6.2" =>
  188.      {
  189.        fn =>
  190.        {
  191.          SYSCONFIG_NW => "/etc/sysconfig/network",
  192.          RESOLV_CONF  => "/etc/resolv.conf"
  193.        },
  194.        table =>
  195.        [
  196.         [ "hostname", \&Utils::Replace::set_sh, SYSCONFIG_NW, HOSTNAME ],
  197.         [ "hostname", \&run_hostname ],
  198.         [ "domain", \&Utils::Replace::set_sh, SYSCONFIG_NW, DOMAIN ],
  199.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  200.        ]
  201.      },
  202.  
  203.      "redhat-7.2" =>
  204.      {
  205.        fn =>
  206.        {
  207.          SYSCONFIG_NW => ["/etc/sysconfig/networking/profiles/default/network",
  208.                           "/etc/sysconfig/networking/network",
  209.                           "/etc/sysconfig/network"],
  210.          RESOLV_CONF  => ["/etc/sysconfig/networking/profiles/default/resolv.conf",
  211.                           "/etc/resolv.conf"],
  212.        },
  213.        table =>
  214.        [
  215.             [ "hostname", \&Utils::Replace::set_sh, SYSCONFIG_NW, HOSTNAME ],
  216.         [ "hostname", \&run_hostname ],
  217.         [ "domain", \&Utils::Replace::set_sh, SYSCONFIG_NW, DOMAIN ],
  218.             [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  219.            ]
  220.      },
  221.  
  222.      "debian-2.2" =>
  223.      {
  224.        fn =>
  225.        {
  226.          RESOLV_CONF => "/etc/resolv.conf",
  227.          HOSTNAME    => "/etc/hostname",
  228.        },
  229.        table =>
  230.        [
  231.         [ "hostname", \&Utils::Replace::set_first_line, HOSTNAME ],
  232.         [ "hostname", \&run_hostname ],
  233.         [ "domain",    \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ]
  234.        ]
  235.      },
  236.  
  237.      "suse-9.0" =>
  238.      {
  239.        fn =>
  240.        {
  241.          RESOLV_CONF  => "/etc/resolv.conf",
  242.          HOSTNAME     => "/etc/HOSTNAME",
  243.        },
  244.        table =>
  245.        [
  246.         [ "hostname", \&Utils::Replace::set_fq_hostname, HOSTNAME, "%hostname%", "%domain%" ],
  247.         [ "hostname", \&run_hostname ],
  248.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  249.        ]
  250.      },
  251.     
  252.      "gentoo" =>
  253.      {
  254.        fn =>
  255.        {
  256.          HOSTNAME    => "/etc/conf.d/hostname",
  257.          DOMAINNAME  => "/etc/conf.d/domainname",
  258.          RESOLV_CONF => "/etc/resolv.conf",
  259.        },
  260.        table =>
  261.        [
  262.         [ "hostname", \&Utils::Replace::set_sh, HOSTNAME, HOSTNAME ],
  263.         [ "hostname", \&run_hostname ],
  264.         [ "domain", \&Utils::Replace::set_sh, DOMAINNAME, DNSDOMAIN ],
  265.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  266.        ]
  267.      },
  268.  
  269.      "freebsd-5" =>
  270.      {
  271.        fn =>
  272.        {
  273.          RC_CONF     => "/etc/rc.conf",
  274.          RESOLV_CONF => "/etc/resolv.conf",
  275.        },
  276.        table =>
  277.        [
  278.         [ "hostname", \&Utils::Replace::set_sh, RC_CONF, hostname, "%hostname%.%domain%" ],
  279.         [ "hostname", \&run_hostname, "%hostname%.%domain%" ],
  280.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  281.        ]
  282.      },
  283.  
  284.      "solaris-2.11" =>
  285.      {
  286.        fn =>
  287.        {
  288.          NODENAME    => "/etc/nodename",
  289.          RESOLV_CONF => "/etc/resolv.conf",
  290.        },
  291.        table =>
  292.        [
  293.         [ "hostname", \&Utils::Replace::set_first_line, NODENAME ],
  294.         [ "domain", \&Utils::Replace::join_first_str, RESOLV_CONF, "domain", "[ \t]+" ],
  295.        ]
  296.      },
  297.    );
  298.  
  299.   my $dist = &get_fqdn_dist ();
  300.   return %{$dist_tables{$dist}} if $dist;
  301.  
  302.   &Utils::Report::do_report ("platform_no_table", $Utils::Backend::tool{"platform"});
  303.   return undef;
  304. }
  305.  
  306. sub add_statichost_alias
  307. {
  308.   my ($localhost, $alias) = @_;
  309.   my $i;
  310.  
  311.   foreach $i (@$localhost)
  312.   {
  313.     return if ($i eq $alias);
  314.   }
  315.   
  316.   push @$localhost, $alias;
  317. }
  318.  
  319. sub remove_statichost_alias
  320. {
  321.   my ($localhost, $alias) = @_;
  322.   my $i;
  323.  
  324.   for ($i = 0; $i < @$localhost; $i++) {
  325.     if ($$localhost[$i] eq $alias)
  326.     {
  327.       delete $$localhost[$i];
  328.       return;
  329.     }
  330.   }
  331. }
  332.   
  333. sub ensure_loopback_statichost
  334. {
  335.   my ($statichost, $hostname, $old_hostname, $lo_ip) = @_;
  336.   my $i;
  337.  
  338.   if (exists $$statichost{$lo_ip})
  339.   {
  340.     my $localhost = $$statichost{$lo_ip};
  341.     &remove_statichost_alias ($localhost, $old_hostname) if ($old_hostname);
  342.     &add_statichost_alias ($localhost, $hostname);
  343.   }
  344.   else
  345.   {
  346.     $$statichost{$lo_ip} = [ ("localhost", "localhost.localdomain", $hostname) ];
  347.   }
  348. }
  349.  
  350. sub get_fqdn
  351. {
  352.   my %dist_attrib;
  353.   my $hash;
  354.  
  355.   %dist_attrib = &get_fqdn_parse_table ();
  356.  
  357.   $hash = &Utils::Parse::get_from_table ($dist_attrib{"fn"},
  358.                                          $dist_attrib{"table"});
  359.  
  360.   return ($$hash {"hostname"}, $$hash{"domain"});
  361. }
  362.  
  363. sub parse_hosts_files
  364. {
  365.   my ($file) = @_;
  366.   my (@arr, %hash, $statichosts, $i);
  367.  
  368.   while (@_)
  369.   {
  370.     $statichosts = &Utils::Parse::split_hash (@_[0], "[ \t]+", "[ \t]+");
  371.     shift @_;
  372.  
  373.     foreach $i (keys %$statichosts)
  374.     {
  375.       $hash{$i} = $$statichosts{$i};
  376.     }
  377.   }
  378.  
  379.   foreach $i (sort keys %hash)
  380.   {
  381.     push @arr, [$i, $hash{$i}];
  382.   }
  383.  
  384.   return \@arr;
  385. }
  386.  
  387. sub get_hosts
  388. {
  389.   return &parse_hosts_files ("/etc/hosts", "/etc/inet/ipnodes") if ($Utils::Backend::tool{"system"} eq "SunOS");
  390.   return &parse_hosts_files ("/etc/hosts");
  391. }
  392.  
  393. sub get_dns
  394. {
  395.   my (@dns);
  396.  
  397.   @dns = &Utils::Parse::split_all_unique_hash_comment ("/etc/resolv.conf", "nameserver", "[ \t]+");
  398.  
  399.   return @dns;
  400. }
  401.  
  402. sub get_search_domains
  403. {
  404.   my (@search_domains);
  405.  
  406.   @search_domains = &Utils::Parse::split_first_array_unique ("/etc/resolv.conf", "search", "[ \t]+", "[ \t]+");
  407.  
  408.   return @search_domains;
  409. }
  410.  
  411. sub set_fqdn
  412. {
  413.   my ($hostname, $domain) = @_;
  414.   my (%dist_attrib, %hash, %old_hash);
  415.  
  416.   $hash{"hostname"} = $hostname;
  417.   $hash{"domain"} = $domain;
  418.  
  419.   ($old_hash{"hostname"}, $old_hash{"domain"}) = &get_fqdn ();
  420.  
  421.   %dist_attrib = &get_fqdn_replace_table ();
  422.   &Utils::Replace::set_from_table ($dist_attrib{"fn"}, $dist_attrib{"table"},
  423.                                    \%hash, \%old_hash);
  424. }
  425.  
  426. sub ensure_hostname
  427. {
  428.   my ($hosts, $hostname, $domain, $old_hostname, $olddomain) = @_;
  429.   my ($fqdn, $old_fqdn, $i);
  430.  
  431.   $fqdn  = $hostname;
  432.   $fqdn .= ".$domain" if ($domain);
  433.  
  434.   $old_fqdn  = $old_hostname;
  435.   $old_fqdn .= ".$old_domain" if ($old_domain);
  436.  
  437.   foreach $i (@$hosts)
  438.   {
  439.     if ($i eq $old_fqdn)
  440.     {
  441.       $i = $fqdn;
  442.     }
  443.   }
  444. }
  445.  
  446. sub set_hosts
  447. {
  448.   my ($config, $hostname, $domain) = @_;
  449.   my ($old_hostname, $old_domain) = &get_fqdn ();
  450.   my ($i, %hash);
  451.  
  452.   foreach $i (@$config)
  453.   {
  454.     &ensure_hostname ($$i[1], $hostname, $domain, $old_hostname, $old_domain);
  455.     $hash{$$i[0]} = $$i[1];
  456.   }
  457.  
  458.   if ($Utils::Backend::tool {"system"} eq "SunOS")
  459.   {
  460.     &Utils::Replace::join_hash ("/etc/inet/ipnodes", "[ \t]+", "[ \t]+", \%hash);
  461.  
  462.     # save only IPv4 entries in /etc/hosts
  463.     foreach $i (keys %hash)
  464.     {
  465.       delete $hash{$i} if ($i =~ /[a-fA-F:]/);
  466.     }
  467.  
  468.     &Utils::Replace::join_hash ("/etc/hosts", "[ \t]+", "[ \t]+", \%hash);
  469.   }
  470.   else
  471.   {
  472.     &Utils::Replace::join_hash ("/etc/hosts", "[ \t]+", "[ \t]+", \%hash);
  473.   }
  474. }
  475.  
  476. sub set_dns
  477. {
  478.   my ($dns) = @_;
  479.  
  480.   &Utils::Replace::join_all ("/etc/resolv.conf", "nameserver", "[ \t]+", $dns);
  481. }
  482.  
  483. sub set_search_domains
  484. {
  485.   my ($search_domains) = @_;
  486.  
  487.   &Utils::Replace::join_first_array ("/etc/resolv.conf", "search",
  488.                                      "[ \t]+", "[ \t]+", $search_domains);
  489. }
  490.  
  491. sub get_files
  492. {
  493.   my (%dist_attrib, $fn, $f, $files);
  494.  
  495.   %dist_attrib = &get_fqdn_parse_table ();
  496.   $fn = $dist_attrib {"fn"};
  497.  
  498.   foreach $f (values %$fn)
  499.   {
  500.     push @$files, $f;
  501.   }
  502.  
  503.   return $files;
  504. }
  505.  
  506. 1;
  507.